package main

import "fmt"

func main() {
	// test1()
	test2()
}

func test1() {
	// Channel用作信号(Signal)的场景（信号量）
	fmt.Println("Begin doing something!")
	c := make(chan bool)
	go func() {
		fmt.Println("Doing something…")
		close(c)
	}()
	<-c
	fmt.Println("Done!")
}

func test2() {
	// 协同多个Goroutines
	start := make(chan bool)
	for i := 1; i <= 100; i++ {
		// 这些Goroutine在被创建出来后都阻塞在"<-start"上
		go worker(start, i)
	}
	close(start)
	select {} //deadlock we expected
}

func worker(start chan bool, index int) {
	<-start
	fmt.Println("This is Worker:", index)
}
